package com.wiretap.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.wiretap.dao.RemoteCallDao;
import com.wiretap.po.RemoteCall;
import com.wiretap.util.Pager;

public class RemoteCallDaoImpl implements RemoteCallDao {
	private SessionFactory sessionFactory;

	@Override
	public void addRemoteCall(RemoteCall call) throws Exception {
		try {
			this.sessionFactory.getCurrentSession().persist(call);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("exception occured while addRemoteCall");
		}
	}

	@Override
	public int deleteRemoteCall(Long id) throws Exception {
		try {
			return this.sessionFactory.getCurrentSession().createQuery("delete from RemoteCall r where r.id=:id").setLong("id", id).executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("exception occured while deleteRemoteCall");
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pager<RemoteCall> getUserRemoteCall(String ownerPhone, int currentPage, int pageSize) throws Exception {
		try {
			Session sesion = this.sessionFactory.getCurrentSession();
			Query query = sesion.createQuery("select count(*) from RemoteCall r where r.ownerPhone =:ownerPhone").setString("ownerPhone", ownerPhone);
			int totalCount = ((Number) query.uniqueResult()).intValue();
			int pageCount = (totalCount % pageSize == 0) ? totalCount / pageSize : (totalCount / pageSize) + 1;
			List<RemoteCall> list = sesion.createQuery("from RemoteCall r where r.ownerPhone =:ownerPhone order by r.addTime desc").setString("ownerPhone", ownerPhone)
					.setFirstResult((currentPage - 1) * pageSize).setMaxResults(pageSize).list();
			Pager<RemoteCall> pager = new Pager<RemoteCall>(list, currentPage, totalCount, pageCount);
			return pager;
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("getUserRemoteCall error");
		}
	}

	@Override
	public RemoteCall getRemoteCallById(Long id) throws Exception {
		try {
			return (RemoteCall) sessionFactory.getCurrentSession().get(RemoteCall.class, id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("getRemoteCallById error");
		}
	}

	@Override
	public void updateRemoteCall(RemoteCall call) throws Exception {
		try {
			sessionFactory.getCurrentSession().update(call);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("updateRemoteCall error");
		}
	}

	@Override
	public int updateRemoteCallExecutedStatus(Long[] ids, boolean excuted) throws Exception {
		try {
			String hql = "update RemoteCall a set  a.excuted=? where ";
			for (int i = 0; i < ids.length; i++) {
				hql += "a.id=?";
				if (i != ids.length - 1) {
					hql += " or ";
				}
			}
			Query query = sessionFactory.getCurrentSession().createQuery(hql);
			query.setBoolean(0, excuted);
			for (int i = 0; i < ids.length; i++) {
				query.setLong(i + 1, ids[i]);
			}
			return query.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("updateRemoteCallExecutedStatus error");
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RemoteCall> getAllUnexecutedRemoteCall(String ownerPhone) throws Exception {
		try {
			Session sesion = this.sessionFactory.getCurrentSession();
			List<RemoteCall> list = sesion.createQuery("from RemoteCall a where a.ownerPhone = :ownerPhone and a.excuted = :excuted").setString("ownerPhone", ownerPhone).setBoolean("excuted", false)
					.list();
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("getAllUnexecutedRemoteCall error");
		}
	}

	@Override
	public int deleteRemoteCallByOwnerPhone(String ownerPhone) throws Exception {
		try {
			return sessionFactory.getCurrentSession().createQuery("delete from RemoteCall a where a.ownerPhone = :ownerPhone ").setString("ownerPhone", ownerPhone).executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("deleteRemoteCallByOwnerPhone发生异常");
		}
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

}
